/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mtetris;
import java.awt.Toolkit;
import java.sql.Date;
import java.text.*;
import java.lang.Math;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
/**
 *
 * @author Administrator
 */
public class tetriswin extends Frame implements KeyListener{
    int status = 0;
    int level = 1;
    private startwin start = new startwin();
    private gamewin game = new gamewin();
    java.util.Random randnum=new java.util.Random(System.currentTimeMillis());
    private java.util.Timer Ttimer = new Timer();

    public tetriswin(){
        super("MTetris");
        addWindowListener(new WindowCloser());
        this.setSize(400, 600);
        addKeyListener(this);
        this.setVisible(true);
        add("Center", game);
        add("Center", start);
        game.setVisible(false);
        start.setVisible(true);
        Ttimer.schedule(
            new java.util.TimerTask(){
                public void run(){
                    if(game.downAble()==1)game.getCurrent().setPos(game.getCurrent().getX(), game.getCurrent().getY()+20);
                    else{
                        game.doneCurrentBlock();
                        game.setCurrent(Math.abs(randnum.nextInt()%7), Math.abs(randnum.nextInt()%4));
                        game.getCurrent().setPos();
                    }
                    game.repaint();
                }
            }
            , 0,1000/level);
    }
    
    public void keyPressed(KeyEvent ke){
        if(status == 0){
            if(ke.getKeyCode()==10){
                if(start.getOpt()==0){
                    status = 1;
                    start.setVisible(false);
                    game.setVisible(true);
                    game.gameInit();
                    game.setCurrent(Math.abs(randnum.nextInt()%7), Math.abs(randnum.nextInt()%4));
                    
                }
                else System.exit(0);
            }
            else if(ke.getKeyCode()==38)
                start.setOpt();
            else if(ke.getKeyCode()==40)
                start.setOpt();
            else if(ke.getKeyCode()==27)
                System.exit(0);
        }
        if(status == 1){
            int currentX, currentY;
            currentX = game.getCurrent().getX();
            currentY = game.getCurrent().getY();
            
            if(ke.getKeyCode()==27){
                game.setVisible(false);
                start.setVisible(true);
                //Ttimer.cancel();
                status = 0;
            }
            else if(ke.getKeyCode()==40 && game.downAble()==1){
                game.getCurrent().setPos(currentX, currentY+20);
                game.repaint();
            }
            else if(ke.getKeyCode()==39 && game.rightAble()==1){
                game.getCurrent().setPos(currentX+20, currentY);
                game.repaint();
            }
            else if(ke.getKeyCode()==37 && game.leftAble()==1){
                game.getCurrent().setPos(currentX-20, currentY);
                game.repaint();
            }
            else if(ke.getKeyCode()==38 && game.changeAble()==1){
                game.setCurrent(game.getCurrentBlock(), (game.getCurrentStyle()+1)%4);
                game.getCurrent().setPos(currentX, currentY);
                game.repaint();
            }
            else if(ke.getKeyCode()==32){
                while(game.downAble()==1)
                    game.getCurrent().setPos(game.getCurrent().getX(), game.getCurrent().getY()+20);
                game.doneCurrentBlock();
                game.setCurrent(Math.abs(randnum.nextInt()%7), Math.abs(randnum.nextInt()%4));
                game.getCurrent().setPos();
                //game.getCurrent().setPos(90, 90);
                game.repaint();
            }
        }
        
    }
    public void keyReleased(KeyEvent ke){
    }
    public void keyTyped(KeyEvent ke){
    }
    
    private class WindowCloser extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent we){
            System.exit(0);
        }
    }
}
